
import { Label, _decorator } from 'cc';
import { app } from '../../../../app-core/App';
import { injectPresenter } from '../../../../app-core/inject';
import { BaseView } from '../../../../app-core/mvp/base/BaseView';
import { UIID } from '../../../UIConfigs';
import { LoginViewer } from '../../login/viewer/LoginViewer';
import * as LoadingContract from '../contract/LoadingContract';
import LoadingPresenter from '../presenter/LoadingPresenter';
const { ccclass, property } = _decorator;

@ccclass('LoadingViewer')
export class LoadingViewer extends BaseView implements LoadingContract.ILoadingView {
    @property({ type: Label })
    private loadingTips: Label;

    @property({ type: Label })
    private loadingProgress: Label;

    @injectPresenter(LoadingPresenter)
    loadingPresenter: LoadingContract.ILoadingPresenter;


    data: any = {
        /** 加载资源当前进度 */
        finished: 0,
        /** 加载资源最大进度 */
        total: 0,
        /** 加载资源进度比例值 */
        progress: "",
        /** 加载流程中提示文本 */
        prompt: ""
    };


    public onAdded(): any {
        this.data.prompt = "start";
        app.task.createSync()
            .add((next) => {
                next();
            })
            .start(() => {
                // this.loadingTips.string = "ok";
                this.loadingPresenter.startLoading(
                    (cur, total) => {
                        this.data.prompt = 'loading...';
                        // this.loadingProgress.string = cur/total * 100 + "%";
                        this.data.finished = cur;
                        this.data.total = total;
                        this.data.progress = (cur / total * 100).toFixed(2) + "%";
                    },
                    (totaol) => {
                        // this.loadingTips.string = "ok";
                        this.data.prompt = "ok";

                        setTimeout(() => {
                            this.removeSelf(true);
                            app.ui.open(UIID.Login, { viewer: LoginViewer })
                        }, 1000);
                    });
            })
    }
}